/*
 * This file is part of ELCube.
 *
 * ELCube is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ELCube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
 */
package cn.nkpro.elcube.mq.controller;

import cn.nkpro.elcube.annotation.NkNote;
import cn.nkpro.elcube.mq.NkMQManagerImpl;
import com.rabbitmq.http.client.domain.InboundMessage;
import com.rabbitmq.http.client.domain.QueueInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by bean on 2020/7/17.
 */
@NkNote("26.[DevOps]队列管理")
@RestController
@RequestMapping("/ops/mq")
@PreAuthorize("hasAnyAuthority('*:*','DEVOPS:*','DEVOPS:MQ')")
@ConditionalOnBean(value = NkMQManagerImpl.class)
public class DevOpsMqController {

    @Autowired@SuppressWarnings("all")
    private NkMQManagerImpl mqManager;

    @RequestMapping(value = "/queues",method = RequestMethod.GET)
    public List<QueueInfo> queues() {
        return mqManager.getQueues();
    }

    @RequestMapping(value = "/queues/{queue}/get",method = RequestMethod.GET)
    public List<InboundMessage> messages(@PathVariable String queue, @RequestParam(value = "rows",defaultValue = "10") Integer rows) {
        return mqManager.getMessages(queue,rows);
    }

    @RequestMapping(value = "/queues/{queue}/resume",method = RequestMethod.POST)
    public void resume(@PathVariable String queue) {
        mqManager.resume(queue);
    }

    @RequestMapping(value = "/queues/{queue}/discard",method = RequestMethod.POST)
    public void discard(@PathVariable String queue) {
        mqManager.discard(queue);
    }

    @RequestMapping(value = "/queues/{queue}/send",method = RequestMethod.POST)
    public void send(@PathVariable String queue, @RequestBody String message) {
        mqManager.publish(null,queue,message);
    }
}
